﻿
using Newtonsoft.Json;
using RTW.OA.Areas.Personal.Servers;
using RTW.OA.Controllers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Areas.Personal.Controllers
{
    public class SettingController : BaseController
    {
	      private SettingServers settingServers;

		    public SettingController()
			{
				settingServers = new SettingServers();
			}


		// GET: Personal/Setting
        public ActionResult Index()
        {
		      		
           return View();
        }

		// GET: Personal/Setting/Edit/5
        public ActionResult Edit(int? id)
        {
			ViewBag.SEXData = settingServers.GetSEX();
            ViewBag.CALL_SOUNDData = settingServers.GetCALL_SOUND();
            ViewBag.NOT_LOGINData = settingServers.GetNOT_LOGIN();
            var model = new user();
            if (id != null)
            {
                ViewBag.Title = "编辑用户";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建用户";
            }
            return View(model);
        }

		// POST: Personal/Setting/Edit/5
        [HttpPost]
        public ActionResult Edit(int? id, user model)
        {
		 ViewBag.SEXData = settingServers.GetSEX();
            ViewBag.CALL_SOUNDData = settingServers.GetCALL_SOUND();
            ViewBag.NOT_LOGINData = settingServers.GetNOT_LOGIN();
        try
            {
                if (id == null || id == 0)
                {
					settingServers.Add(model);
                    ViewBag.msg = "新增用户信息成功！";
                    model = new user();

                }
                else
                {
					settingServers.Update(model);
                    ViewBag.msg = "修改用户信息成功！";

                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }


        public ActionResult ChangPwd(int? id)
        {
            var model = new user();
            if (id == null)
            {
                id = base.CurrentUser.USER_ID;
            }
            model = GetModel(id.Value.ToString());
            ViewBag.Title = "修改密码";
     
            return View(model);
        }
        // POST: Personal/Setting/Edit/5
        [HttpPost]
        public ActionResult ChangPwd(int? id, user model)
        {
            try
            {
                if (id == null || id == 0)
                {
                    ViewBag.msg = "修改用户信息失败！";
                }
                else
                {
                    if (!string.IsNullOrEmpty(model.OLD_USER_PWD))
                    {
                        //加密密码
                        model.OLD_USER_PWD = DEncryptUtil.Md5Encrypt(model.OLD_USER_PWD);

                        var isPwd = settingServers.CheckPwd(model);
                        if (isPwd)
                        {
                            //加密密码
                            model.USER_PWD = DEncryptUtil.Md5Encrypt(model.USER_PWD);

                            settingServers.UpdatePwd(model);
                            ViewBag.msg = "修改用户信息成功！";
                        }
                        else {
                            ViewBag.msg = "原密码错误！";
                        }
                    }
                    else
                    {
                        ViewBag.msg = "原密码不为空！";
                    }
                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }


        // GET: Personal/Setting/Delete/5
        public ActionResult Delete(int id)
        {
		    if (id != 0)
            {
                settingServers.Del(id.ToString());
                ViewBag.msg = "删除用户表信息成功！";                   
            }    
            return Content("");
        }


		 #region //GetData
		public string GetTableJsonResult(
		      				   )
        {
            var list = settingServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

		public user GetModel(string id)
        {
            var model = settingServers.GetModelById(id);
            return model;
        }

		 #endregion //GetData
	}
}